package com.obggtc.picture.controller;

import cn.hutool.crypto.digest.BCrypt;
import com.obggtc.picture.common.R;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 */
@RestController
@RequestMapping("/test")
public class TestController {

    /**
     * 测试认证接口
     * @return 当前用户信息
     */
    @GetMapping("/auth")
    public R<Map<String, Object>> testAuth() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        
        Map<String, Object> result = new HashMap<>();
        result.put("username", authentication.getName());
        result.put("authenticated", authentication.isAuthenticated());
        result.put("authorities", authentication.getAuthorities());
        
        return R.ok(result, "认证成功");
    }

    public static void main(String[] args) {
        String assword = "1qaz@WSX";

        // 生成带随机盐的哈希（10 为 cost）
        String salt = BCrypt.gensalt(10); // 内含随机盐和 cost 信息
        String hashed = BCrypt.hashpw(assword, salt);
        System.out.println("hashed = " + hashed); // 整条存入 DB

        // 已知哈希
        String hash = "$2a$10$zQ4QPD1824oit9YJsyjIn.DZcsQ0tJlCfMafijWmonPvkjUeuaHai";

        // 要验证的密码
        String rawPassword = "1qaz@WSX";

        boolean matches = BCrypt.checkpw(rawPassword, hash);
        System.out.println("密码是否匹配: " + matches);



    }
} 